/*
========================================================================
 Name        : EasyPlayerContainerView.cpp
 Author      : Yupeng Song
 Copyright   : This is GUI hello world for Easy Symbian
 Description : 
========================================================================
*/

#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <eikbtgpc.h>
#include <EasyPlayer.rsg>

#include "EasyPlayer.hrh"
#include "EasyPlayerPlaybackView.h"
#include "EasyPlayerPlaybackContainer.hrh"
#include "EasyPlayerPlaybackContainer.h"
#include "EasyPlayerVideoPlayer.h"

_LIT( KMediaFile, "c:\\data\\Videos\\01022010004.3gp" );
/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CEasyPlayerPlaybackView::CEasyPlayerPlaybackView()
	{
	iPlaybackContainer = NULL;
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CEasyPlayerPlaybackView::~CEasyPlayerPlaybackView()
	{
	iVideoPlayer->Cancel();
	delete iVideoPlayer;
	iVideoPlayer = NULL;
	delete iPlaybackContainer;
	iPlaybackContainer = NULL;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CEasyPlayerPlaybackView
 */
CEasyPlayerPlaybackView* CEasyPlayerPlaybackView::NewL()
	{
	CEasyPlayerPlaybackView* self = CEasyPlayerPlaybackView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CEasyPlayerPlaybackView
 */
CEasyPlayerPlaybackView* CEasyPlayerPlaybackView::NewLC()
	{
	CEasyPlayerPlaybackView* self = new ( ELeave ) CEasyPlayerPlaybackView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}


/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */ 
void CEasyPlayerPlaybackView::ConstructL()
	{
	BaseConstructL( R_EASY_PLAYER_CONTAINER_EASY_PLAYER_CONTAINER_VIEW );

	}

/**
 * @return The UID for this view
 */
TUid CEasyPlayerPlaybackView::Id() const
	{
	return TUid::Uid( EEasyPlayerPlaybackViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CEasyPlayerPlaybackView::HandleCommandL( TInt aCommand )
	{
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{
		case EEasyPlayerPlaybackViewPlayMenuItemPlayCommand:
			iVideoPlayer->StartPlayFileL( KMediaFile );
			break;
		case EAknSoftkeyExit:
			AppUi()->HandleCommandL( EEikCmdExit );
			break;
		default:
			break;
		}
	}

/**
 *	Handles user actions during activation of the view, 
 *	such as initializing the content.
 */
void CEasyPlayerPlaybackView::DoActivateL( 
		const TVwsViewId& /*aPrevViewId*/,
		TUid /*aCustomMessageId*/,
		const TDesC8& /*aCustomMessage*/ )
	{
	SetupStatusPaneL();

	if ( iPlaybackContainer == NULL )
		{
		iPlaybackContainer = CreateContainerL();
		iPlaybackContainer->SetMopParent( this );
		AppUi()->AddToStackL( *this, iPlaybackContainer );
		}
	
	iVideoPlayer = CEasyPlayerVideoPlayer::NewL( 
			iPlaybackContainer->ClientWsSession(),
			iPlaybackContainer->ScreenDevice(),
			iPlaybackContainer->ClientWindow(),
			iPlaybackContainer->Rect(),
			iPlaybackContainer->Rect() );
	}

/**
 */
void CEasyPlayerPlaybackView::DoDeactivate()
	{
	
	CleanupStatusPane();
	
	if ( iPlaybackContainer != NULL )
		{
		AppUi()->RemoveFromViewStack( *this, iPlaybackContainer );
		delete iPlaybackContainer;
		iPlaybackContainer = NULL;
		}
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CEasyPlayerPlaybackView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();
	
	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	}

void CEasyPlayerPlaybackView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext = 
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context = static_cast< CAknContextPane* > ( 
			StatusPane()->ControlL( contextPaneUid ) );
		context->SetPictureToDefaultL();
		}
	
	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle = 
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane* title = static_cast< CAknTitlePane* >( 
			StatusPane()->ControlL( titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_EASY_PLAYER_CONTAINER_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}				
	}


void CEasyPlayerPlaybackView::CleanupStatusPane()
	{
	}

/**
 *	Creates the top-level container for the view.  You may modify this method's
 *	contents and the CEasyPlayerPlaybackContainer::NewL() signature as needed to initialize the
 *	container, but the signature for this method is fixed.
 *	@return new initialized instance of CEasyPlayerPlaybackContainer
 */
CEasyPlayerPlaybackContainer* CEasyPlayerPlaybackView::CreateContainerL()
	{
	return CEasyPlayerPlaybackContainer::NewL( ClientRect(), NULL, this );
	}

